# -*- coding:utf-8 -*-
# created on 2017/4/30

from mathsolver.functions.base import *
from mathsolver.functions.base.base import new_latex
from mathsolver.functions.mathematica.mathematicaSolve import MathematicaSolve
from mathsolver.functions.xiangliang.Vector_SiBianXing.Vector_Quadra_Axis_Update import XLBuildRectangleAxis
from mathsolver.functions.xiangliang.Vector_SiBianXing.base import (XLQuadraKnownUpdate,
                                                                    XLQuadraAxisConditionEqTransform)
from mathsolver.functions.xiangliang.basic import xl_solutions_subs


class XLQuadraGetVariable001(BaseFunction):
    """
    在矩形ABCD中,\\overrightarrow{OA}=\\overrightarrow{CO},
    \\overrightarrow{AB}+\\overrightarrow{AD}=λ\\overrightarrow{AO},则λ=
    1.矩形类
    2.坐标法
    """
    def solver(self, *args):
        assert len(args) == 2
        p1, p2, p3, p4 = args[0].value
        name = p1 + p2 + p3 + p4
        assert name in self.known
        quadra = self.search(name)
        assert quadra
        quadra_type = quadra.type
        assert quadra_type == "vRectangle"
        new_known = dict(self.known)
        stepsolver1 = XLBuildRectangleAxis(new_known).solver(name)
        self.steps += stepsolver1.steps
        self.label.update(stepsolver1.label)
        quadra = stepsolver1.output[0]
        target = args[1].sympify()
        v_eqs = quadra.Eqs
        u_eqs = [[target, 0]]
        u_eqs.extend(v_eqs)
        stepsolver2 = XLQuadraKnownUpdate(new_known).solver(u_eqs, quadra)
        self.steps += stepsolver2.steps
        new_known = stepsolver2.output[0]
        condition_veqs = []
        for eq in v_eqs:
            if len(eq) == 2:
                new_eqs = XLQuadraAxisConditionEqTransform(new_known).solver(BaseEq(eq)).output[0].sympify()
            else:
                new_eqs = XLQuadraAxisConditionEqTransform(new_known).solver(BaseIneq(eq)).output[0].sympify()
            condition_veqs.extend(new_eqs)
        self.steps.append(["", "依题意，得"])
        if len(condition_veqs) == 1:
            if len(condition_veqs[0]) == 2:
                self.steps.append(["", "%s" % BaseEq(condition_veqs[0]).printing()])
            else:
                self.steps.append(["", "%s" % BaseIneq(condition_veqs[0]).printing()])
        else:
            self.steps.append(["", "%s" % BaseIneqs(condition_veqs).printing()])
        solutions = MathematicaSolve().solver(BaseIneqs(condition_veqs)).output[0]
        self.steps.append(["", "解得: %s" % (solutions.printing())])
        target_values = xl_solutions_subs(solutions, target)
        self.steps.append(["", "∴ %s"
                           % " 或 ".join(["%s = %s" % (new_latex(target), new_latex(v)) for v in target_values])])
        self.output.append(BaseNumbers(target_values))
        self.label.add("向量运算-坐标法")
        return self


class XLQuadraGetVariable(BaseFunction):
    CLS = [XLQuadraGetVariable001]

    def solver(self, *args):
        known = self.known
        r = None
        for cl in XLQuadraGetVariable.CLS:
            try:
                r = cl(known, verbose=True).solver(*args)
                break
            except Exception:
                pass
        if not r:
            raise 'try fail'
        return r
